import { CUSTOMER_INFO, CLEAR_INFO, CUSTOMER_LIST, ADD_CUSTOMER } from "../constants/customer";
import { getJSON, postJSON } from "../service/request";
import Taro from "@tarojs/taro";


export const getCustomerInfo = async () => {
  const { data } = await getJSON({ module: 'OrderService.info' });
  if (data && data.state === '000') {
    return {
      type: CUSTOMER_INFO,
      ...data.data
    }
  } else {
    Taro.showToast({
      title: data.msg,
      icon:'none'
    })
    return {
      type: CUSTOMER_INFO,
    }
  }
}

export const getCustomerList = async params => {
  const { data } = await getJSON({ ...params, module: 'OrderService.list' });
  if (data && data.state === '000') {
    const { data: list } = data;
    list.map(item => {
      item.appointime = item.appointime !== null && item.appointime.substring(0, 10)
      item.orderdate = item.orderdate !== null && item.orderdate.substring(0, 10)
      item.comdate = item.comdate !== null && item.comdate.substring(0, 10)
      item.ctime = item.ctime !== null && item.ctime.substring(0,10)
    })
    return {
      type: CUSTOMER_LIST,
      list,
      page: params.page
    }
  } else {
    Taro.showToast({
      title: data.msg,
      icon:'none'
    })
    return {
      type: CUSTOMER_LIST,
    }
  }
}

export const addCustomer = async params => {
  const { data } = await postJSON({ ...params, module: 'OrderService.add' });
  if (data && data.state === '000') {
    return true
  } else {
    Taro.showToast({
      title: data.msg,
      icon: 'none',
      duration:2000
    })
    return false;
  }
}

export const clearCustomerInfo = () => {
  return {
    type:CLEAR_INFO
  }
}
